<template>
  <page-model
  :breadHeard=breadHeard
  @backLast="backLast"
  >
  <desc-title
  class="mt-20"
  :descTitle=descTitle
  >
  </desc-title>
  <div class="content">
    <el-form :model="tbCameraInformationDTO" :rules="rules" ref="tbCameraInformationDTO" label-width="300px">
        <el-form-item label="摄像机编号：" prop="cameraId">
          <el-input type="text" placeholder="请输入摄像机编号"  v-model="tbCameraInformationDTO.cameraId" disabled></el-input>
        </el-form-item>
        <el-form-item label="摄像头名称：" prop="cameraName">
          <el-input type="text" placeholder="请输入摄像头名称"  v-model="tbCameraInformationDTO.cameraName" ></el-input>
        </el-form-item>
        <el-form-item label="区域：" prop="tbCameraAreaId">
          <el-select v-model="tbCameraInformationDTO.tbCameraAreaId" placeholder="请选择区域"  :disabled="isEdit">
            <el-option
              v-for="item in seletList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
              >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否球机：" prop="isSpeedDome">
          <el-select v-model="tbCameraInformationDTO.isSpeedDome">
            <el-option label="是" :value=1></el-option>
            <el-option label="否" :value=0></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="监测类型：" prop="accidentTypes">
          <el-select v-model="accidentType" placeholder="请选择监测类型">
            <el-option
              v-for="item in accdientList"
              :key="item.typeId"
              :value="item.typeId"
              :label="item.accidentName"
              >
            </el-option>
          </el-select>
          <div class="add" @click="addAccident">添加</div>
          <div class="DeviceType">
            <div class="DeviceTypeList" v-for="(item, index) in accidentNameList" :key="index">
              <div class="DeviceTypeListName">{{ item.accidentName }}</div>
              <i class="el-icon-close" @click="deleteAccident(item)"></i>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="端口：" prop="port">
          <el-input type="text" placeholder="请输入端口号"  v-model="tbCameraInformationDTO.port" ></el-input>
        </el-form-item>
        <el-form-item label="项目播放地址：">
          <el-input type="text" placeholder="请输入项目播放地址"  v-model="tbCameraInformationDTO.playAddress" ></el-input>
        </el-form-item>
        <el-form-item label="公司播放地址：" >
          <el-input type="text" placeholder="请输入公司播放地址"  v-model="tbCameraInformationDTO.innerPlayAddress" ></el-input>
        </el-form-item>
        <el-form-item label="内网 IP地址：" prop="cameraInnerIp">
          <el-input type="text" placeholder="请输入内网 IP地址"  v-model="tbCameraInformationDTO.cameraInnerIp" ></el-input>
        </el-form-item>
        <el-form-item label="WEB IP地址：" prop="cameraWebIp">
          <el-input type="text" placeholder="请输入WEB IP地址"  v-model="tbCameraInformationDTO.cameraWebIp" ></el-input>
        </el-form-item>
        <el-form-item label="APP IP地址：" prop="cameraAppIp">
          <el-input type="text" placeholder="请输入APP IP地址"  v-model="tbCameraInformationDTO.cameraAppIp" ></el-input>
        </el-form-item>
        <el-form-item label="用户名：" prop="cameraUsername">
          <el-input type="text" placeholder="请输入用户名"  v-model="tbCameraInformationDTO.cameraUsername" ></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="cameraPassword">
          <el-input type="password" placeholder="请输入密码"  v-model="tbCameraInformationDTO.cameraPassword" ></el-input>
        </el-form-item>
    </el-form>
    <div class="addBtn">
      <span class="confirm flex-center" @click="confirmAdd">确定</span>
    </div>
  </div>
</page-model>
</template>

<script>
export default {
  data () {
    return {
      breadHeard: {
        title: '添加摄像机',
        firstBread: '行为识别 ',
        secondBread: '添加'
      },
      descTitle: '摄像头信息',
      accdientList: [],
      seletList: [],
      accidentType: '',
      accidentTypes: [],
      accidentNameList: [],
      tbCameraInformationDTO: {
        port: '',
        cameraInnerIp: '',
        accidentTypes: '',
        isSpeedDome: '',
        cameraId: '',
        cameraName: '',
        tbCameraAreaId: '',
        cameraPassword: '',
        cameraUsername: '',
        cameraWebIp: '',
        cameraAppIp: '',
        playAddress: '',
        innerPlayAddress: ''
      },
      rules: {
        isSpeedDome: [
          { required: true, message: '请选择球机', trigger: 'blur' }
        ],
        cameraId: [
          { required: true, message: '请输入摄像机编号', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '请输入端口号', trigger: 'blur' }
        ],
        accidentTypes: [
          { required: true, message: '请添加至少一个监测类型', trigger: 'blur' }
        ],
        cameraName: [
          { required: true, message: '请输入摄像头名称', trigger: 'blur' }
        ],
        tbCameraAreaId: [
          { required: true, message: '请选择区域', trigger: 'blur' }
        ],
        cameraPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        cameraUsername: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        cameraWebIp: [
          { required: true, message: '请输入WEB IP地址', trigger: 'blur' }
        ],
        cameraInnerIp: [
          { required: true, message: '请输入内网 IP地址', trigger: 'blur' }
        ],
        cameraAppIp: [
          { required: true, message: '请输入APP IP地址', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isEdit () {
      return false
    },
    cameraId () {
      return sessionStorage.getItem('cameraId')
    }
  },
  methods: {
    backLast () {
      this.$router.push('/actionMain')
    },
    addAccident () { // 添加监测类型
      if (this.accidentNameList.indexOf(this.accdientList.filter(item => item.typeId === this.accidentType)[0]) !== -1) {
        this.$message({
          type: 'error',
          message: '无法重复添加',
          duration: 1000
        })
        return
      }
      this.accdientList.length && this.accidentNameList.push(this.accdientList.filter(item => item.typeId === this.accidentType)[0])
      this.accidentTypes.push(this.accidentType)
      this.tbCameraInformationDTO.accidentTypes = [...this.accidentTypes]
      // console.log(this.accidentTypes)
      // delete this.rules.accidentTypes
    },
    deleteAccident (accdient) {
      this.accidentNameList = this.accidentNameList.filter(item => item.typeId !== accdient.typeId)
      this.tbCameraInformationDTO.accidentTypes = this.tbCameraInformationDTO.accidentTypes.filter(item => item !== accdient.typeId)
      this.accidentTypes = this.accidentTypes.filter(item => item !== accdient.typeId)
    },
    async createDeviceNum () {
      const numData = await this.$store.dispatch('actionRecognition/createDeviceNum')
      this.accidentType = this.accdientList[0].typeId
      this.tbCameraInformationDTO = {
        isSpeedDome: 0,
        cameraId: numData.message,
        tbCameraAreaId: this.seletList[0] ? +this.seletList[0].id : '',
        cameraPassword: '',
        accidentTypes: '',
        cameraName: '',
        cameraUsername: '',
        cameraWebIp: '',
        cameraAppIp: '',
        cameraInnerIp: '',
        playAddress: '',
        innerPlayAddress: ''
      }
      // console.log(this.seletList)
    },
    async judgePageType () { // 判断添加/编辑
      const listData = await this.$store.dispatch('actionRecognition/getCateList')
      const { data } = await this.$store.dispatch('actionRecognition/getAccidentData')
      this.accdientList = data
      // console.log(this.listData)
      this.seletList = listData && listData.data.filter((item, index) => index > 0)
      if (this.cameraId) { // 编辑
        this.breadHeard = {
          title: '编辑摄像机',
          firstBread: '行为识别 ',
          secondBread: '编辑'
        }
        this.$store.dispatch('actionRecognition/getCurrentCamera', { id: this.cameraId }).then(res => {
          if (res.data.code === 200) {
            this.accidentType = this.accdientList[0].typeId
            const data = res.data.data
            this.accidentNameList = data.accidentTypeIds.length && data.accidentTypeIds.map(item => {
              return this.accdientList.filter(it => it.typeId === item)[0]
            }
            )
            // console.log(this.accidentNameList)
            this.accidentTypes = data.accidentTypeIds
            this.tbCameraInformationDTO = {
              cameraInnerIp: data.cameraInnerIp,
              isSpeedDome: data.isSpeedDome,
              accidentTypes: data.accidentTypeIds,
              cameraName: data.cameraName,
              port: data.port,
              cameraId: data.cameraId,
              tbCameraAreaId: data.tbCameraAreaId,
              cameraPassword: data.cameraPassword,
              cameraUsername: data.cameraUsername,
              cameraWebIp: data.cameraWebIp,
              cameraAppIp: data.cameraAppIp,
              playAddress: data.playAddress,
              innerPlayAddress: data.innerPlayAddress
            }
          }
        })
        return
      }
      this.createDeviceNum()
    },
    confirmAdd () {
      // console.log(this.tbCameraInformationDTO)
      const params = { ...this.tbCameraInformationDTO, id: this.cameraId ? this.cameraId : '' }
      params.accidentTypes = Array.from(new Set([...params.accidentTypes]))
      this.$refs.tbCameraInformationDTO.validate((valid) => {
        if (valid) {
          this.cameraId && this.$store.dispatch('actionRecognition/updateCamera', params).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '编辑成功',
                duration: 500
              })
              this.$router.push('/actionMain')
            } else {
              this.$message({
                type: 'error',
                message: res.data.message,
                duration: 1000
              })
            }
          })
          !this.cameraId && this.$store.dispatch('actionRecognition/addCamera', params).then(res => {
            if (res.data.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功',
                duration: 500
              })
              this.$router.push('/actionMain')
            } else {
              this.$message({
                type: 'error',
                message: res.data.message,
                duration: 1000
              })
            }
          })
        }
      })
    }
  },
  mounted () {
    this.judgePageType()
  }
}
</script>

<style lang="scss" scoped>
.content{
  position: relative;
  background-color: #fff;
  min-height: calc(100% - 201px);
  margin-bottom: 30px;
  ::v-deep .el-form{
    width: 800px;
    .el-input{
      width: 400px;
      .el-input__inner{
        height: 48px;
        line-height: 48px;
      }
    }
  .el-input.is-disabled .el-input__inner{
    background:rgba(248,248,248,1);
    border-radius:6px;
    border:1px solid rgba(220,220,220,1);
    color: #333333;
  }
  .el-form-item{
    margin-bottom: 32px;
    .el-form-item__content{
      position: relative;
    }
  }
  .el-form-item__label{
    text-align: right;
    width: 300px !important;
    }
  .el-form-item__error{
    padding-left: 300px;
  }
  .el-form-item.is-required:not(.is-no-asterisk)>.el-form-item__label:before{
          content: '*';
          color: #4868FF;
          margin-right: 4px;
    }
  }
 ::v-deep .el-upload--picture-card{
    width:390px;
    height:220px;
    background-color: #fff;
    .nopic{
      height: 100%;
      align-items: center;
      justify-content: center;
      position: relative;
      .addpics{
        line-height: 20px;
        height:20px;
        font-size:14px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(200,200,200,1);
      }
    }
          .pictips{
            line-height: 20px;
            position: absolute;
            top: 0;
            left: 586px;
            height:20px;
            font-size:14px;
            font-family:PingFangSC-Regular,PingFang SC;
            font-weight:400;
            color:rgba(255,180,21,1);
            cursor:default
          }
          .el-progress-circle{
            width:40px !important;
            height:40px !important;
          }
      }
    }
    .addBtn{
      padding: 150px 0 30px 300px ;
      .confirm{
        width:172px;
        height:48px;
        background:rgba(72,104,255,1);
        border-radius:24px;
        font-size:18px;
        font-family:PingFangSC-Regular,PingFang SC;
        font-weight:400;
        color:rgba(255,255,255,1);
        cursor: pointer;
      }
    }
    .add {
        position: absolute;
        top: 6px;
        right: 0;
        cursor: pointer;
        width: 84px;
        height: 36px;
        background: rgba(72, 104, 255, 1);
        border-radius: 18px;
        line-height: 36px;
        text-align: center;
        font-size: 14px;
        color: rgba(255, 255, 255, 1);
      }
    .DeviceType {
      margin: 20px 0 0 300px;
      width: 560px;
      height: 240px;
      border-radius: 5px;
      border: 1px solid rgba(230, 230, 230, 1);
      .DeviceTypeList {
        float: left;
        height: 36px;
        border: 1px solid rgba(242, 242, 242, 1);
        margin-left: 12px;
        margin-top: 12px;
        .DeviceTypeListName {
          font-size: 14px;
          float: left;
          line-height: 36px;
          padding-left: 10px;
        }
        .el-icon-close {
          cursor: pointer;
          padding-left: 24px;
          float: left;
          font-size: 15px;
          line-height: 36px;
          padding-right: 12px;
        }
      }
      .DeviceTypeList:hover{
                border-color: #FF2B2B !important;
        .el-icon-close {
          color:#FF2B2B !important;
        }
      }
    }
</style>
